package com.zh.mp.demo.config;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * 验证码 生成的bean
 */
@Configuration
public class MyProducer {

    private static final String IMAGE_WIDTH = "100";
    private static final String IMAGE_HEIGHT = "30";
    private static final String FONT_SIZE = "22";
    private static final String CHAR_LENGTH = "4";
    private static final String CHAR_SPACE = "6";
    private static final String FONT_COLOR = "black";
    private static final String BORDER_COLOR = "LIGHT_GRAY";
    private static final String BACKGROUND_CLR_FROM = "WHITE";
    private static final String NOISE_IMPL = "com.google.code.kaptcha.impl.NoNoise";
    private static final String OBSCURIFICATOR_IMPL = "com.google.code.kaptcha.impl.ShadowGimpy";
    private static final String CHAR_STRING = "0123456789";
    private static final String SESSION_CONFIG_KEY = "checkCode";

    @Bean
    public DefaultKaptcha captchaProducer() {
        DefaultKaptcha captchaProducer = new DefaultKaptcha();
        Properties properties = getProperties();
        Config config = new Config(properties);
        captchaProducer.setConfig(config);

        return captchaProducer;
    }

    public Properties getProperties(){
        Properties properties = new Properties();
        properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, IMAGE_WIDTH);
        properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, IMAGE_HEIGHT);
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, FONT_SIZE);
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, CHAR_LENGTH);
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, CHAR_SPACE);
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, FONT_COLOR);
        properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, BORDER_COLOR);
        properties.setProperty(Constants.KAPTCHA_BACKGROUND_CLR_FROM, BACKGROUND_CLR_FROM);
        properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, NOISE_IMPL);
        properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, OBSCURIFICATOR_IMPL);
        properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, CHAR_STRING);
        properties.setProperty(Constants.KAPTCHA_SESSION_CONFIG_KEY, SESSION_CONFIG_KEY);

        return properties;
    }
}
